<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>管理员仪表板 - 宠物医院管理系统</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
    <style>
        body {
            background-color: #f8f9fa;
            font-family: 'Microsoft YaHei', sans-serif;
        }
        .sidebar {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            color: white;
            position: fixed;
            top: 0;
            left: 0;
            width: 250px;
            z-index: 1000;
            transition: all 0.3s ease;
        }
        .sidebar-header {
            padding: 20px;
            text-align: center;
            border-bottom: 1px solid rgba(255,255,255,0.1);
        }
        .sidebar-header h4 {
            margin: 0;
            font-weight: 600;
        }
        .sidebar-menu {
            padding: 20px 0;
        }
        .sidebar-menu a {
            display: block;
            padding: 15px 25px;
            color: rgba(255,255,255,0.8);
            text-decoration: none;
            transition: all 0.3s ease;
            border-left: 3px solid transparent;
        }
        .sidebar-menu a:hover,
        .sidebar-menu a.active {
            background: rgba(255,255,255,0.1);
            color: white;
            border-left-color: white;
        }
        .sidebar-menu i {
            width: 20px;
            margin-right: 10px;
        }
        .main-content {
            margin-left: 250px;
            padding: 20px;
        }
        .top-navbar {
            background: white;
            padding: 15px 30px;
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            margin-bottom: 30px;
            display: flex;
            justify-content: between;
            align-items: center;
        }
        .welcome-text {
            font-size: 1.5rem;
            font-weight: 600;
            color: #333;
        }
        .user-info {
            display: flex;
            align-items: center;
            margin-left: auto;
        }
        .user-avatar {
            width: 40px;
            height: 40px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            color: white;
            margin-right: 10px;
        }
        .stats-cards {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
            gap: 20px;
            margin-bottom: 30px;
        }
        .stat-card {
            background: white;
            border-radius: 15px;
            padding: 25px;
            box-shadow: 0 5px 15px rgba(0,0,0,0.1);
            transition: transform 0.3s ease;
        }
        .stat-card:hover {
            transform: translateY(-5px);
        }
        .stat-icon {
            width: 60px;
            height: 60px;
            border-radius: 15px;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 24px;
            color: white;
            margin-bottom: 15px;
        }
        .stat-icon.users { background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); }
        .stat-icon.pets { background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%); }
        .stat-icon.appointments { background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%); }
        .stat-icon.records { background: linear-gradient(135deg, #43e97b 0%, #38f9d7 100%); }
        .stat-number {
            font-size: 2rem;
            font-weight: 700;
            color: #333;
            margin-bottom: 5px;
        }
        .stat-label {
            color: #666;
            font-size: 0.9rem;
        }
        .quick-actions {
            background: white;
            border-radius: 15px;
            padding: 25px;
            box-shadow: 0 5px 15px rgba(0,0,0,0.1);
            margin-bottom: 30px;
        }
        .quick-actions h5 {
            margin-bottom: 20px;
            color: #333;
            font-weight: 600;
        }
        .action-btn {
            display: inline-block;
            padding: 12px 20px;
            margin: 5px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            text-decoration: none;
            border-radius: 10px;
            transition: all 0.3s ease;
            font-size: 0.9rem;
        }
        .action-btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(102, 126, 234, 0.4);
            color: white;
        }
        @media (max-width: 768px) {
            .sidebar {
                width: 100%;
                height: auto;
                position: relative;
            }
            .main-content {
                margin-left: 0;
            }
        }
    </style>
</head>
<body>
    <!-- 侧边栏 -->
    <div class="sidebar">
        <div class="sidebar-header">
            <i class="fas fa-paw fa-2x mb-2"></i>
            <h4>宠物医院管理</h4>
            <small>管理员控制台</small>
        </div>
        <div class="sidebar-menu">
            <a href="${pageContext.request.contextPath}/pages/admin/dashboard.jsp" class="active">
                <i class="fas fa-tachometer-alt"></i>仪表板
            </a>
            <a href="${pageContext.request.contextPath}/user/list">
                <i class="fas fa-users"></i>用户管理
            </a>
            <a href="${pageContext.request.contextPath}/pet/list">
                <i class="fas fa-paw"></i>宠物管理
            </a>
            <a href="${pageContext.request.contextPath}/appointment/list">
                <i class="fas fa-calendar-check"></i>预约管理
            </a>
            <a href="${pageContext.request.contextPath}/health/list">
                <i class="fas fa-clipboard-list"></i>健康记录
            </a>
            <a href="${pageContext.request.contextPath}/user/logout">
                <i class="fas fa-sign-out-alt"></i>退出登录
            </a>
        </div>
    </div>

    <!-- 主内容区 -->
    <div class="main-content">
        <!-- 顶部导航 -->
        <div class="top-navbar">
            <div class="welcome-text">
                <i class="fas fa-home me-2"></i>管理员仪表板
            </div>
            <div class="user-info">
                <div class="user-avatar">
                    <i class="fas fa-user"></i>
                </div>
                <div>
                    <div style="font-weight: 600;">${sessionScope.currentUser.realName}</div>
                    <small class="text-muted">管理员</small>
                </div>
            </div>
        </div>

        <!-- 统计卡片 -->
        <div class="stats-cards">
            <div class="stat-card">
                <div class="stat-icon users">
                    <i class="fas fa-users"></i>
                </div>
                <div class="stat-number" id="userCount">-</div>
                <div class="stat-label">注册用户</div>
            </div>
            <div class="stat-card">
                <div class="stat-icon pets">
                    <i class="fas fa-paw"></i>
                </div>
                <div class="stat-number" id="petCount">-</div>
                <div class="stat-label">宠物档案</div>
            </div>
            <div class="stat-card">
                <div class="stat-icon appointments">
                    <i class="fas fa-calendar-check"></i>
                </div>
                <div class="stat-number" id="appointmentCount">-</div>
                <div class="stat-label">预约记录</div>
            </div>
            <div class="stat-card">
                <div class="stat-icon records">
                    <i class="fas fa-clipboard-list"></i>
                </div>
                <div class="stat-number" id="recordCount">-</div>
                <div class="stat-label">健康记录</div>
            </div>
        </div>

        <!-- 快速操作 -->
        <div class="quick-actions">
            <h5><i class="fas fa-bolt me-2"></i>快速操作</h5>
            <a href="${pageContext.request.contextPath}/user/add" class="action-btn">
                <i class="fas fa-user-plus me-2"></i>添加用户
            </a>
            <a href="${pageContext.request.contextPath}/pet/add" class="action-btn">
                <i class="fas fa-plus me-2"></i>添加宠物
            </a>
            <a href="${pageContext.request.contextPath}/appointment/add" class="action-btn">
                <i class="fas fa-calendar-plus me-2"></i>新建预约
            </a>
            <a href="${pageContext.request.contextPath}/health/add" class="action-btn">
                <i class="fas fa-file-medical me-2"></i>添加健康记录
            </a>
        </div>

        <!-- 系统信息 -->
        <div class="row">
            <div class="col-md-6">
                <div class="quick-actions">
                    <h5><i class="fas fa-info-circle me-2"></i>系统信息</h5>
                    <p><strong>系统版本：</strong>1.0.0</p>
                    <p><strong>数据库：</strong>MySQL 8.0</p>
                    <p><strong>服务器：</strong>Apache Tomcat</p>
                    <p><strong>最后登录：</strong><span id="currentTime"></span></p>
                </div>
            </div>
            <div class="col-md-6">
                <div class="quick-actions">
                    <h5><i class="fas fa-chart-pie me-2"></i>今日概览</h5>
                    <p><strong>今日新增用户：</strong><span id="todayUsers">0</span></p>
                    <p><strong>今日新增宠物：</strong><span id="todayPets">0</span></p>
                    <p><strong>今日预约：</strong><span id="todayAppointments">0</span></p>
                    <p><strong>待处理预约：</strong><span id="pendingAppointments">0</span></p>
                </div>
            </div>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
    <script>
        // 显示当前时间
        function updateTime() {
            const now = new Date();
            document.getElementById('currentTime').textContent = now.toLocaleString('zh-CN');
        }
        updateTime();
        setInterval(updateTime, 1000);

        // 加载统计数据
        loadStatistics();
        loadTodayStatistics();

        // 定期刷新数据（每5分钟）
        setInterval(function() {
            loadStatistics();
            loadTodayStatistics();
        }, 300000);

        function loadStatistics() {
            fetch('${pageContext.request.contextPath}/dashboard/stats')
                .then(response => response.json())
                .then(data => {
                    if (data.error) {
                        console.error('获取统计数据失败:', data.error);
                        return;
                    }

                    document.getElementById('userCount').textContent = data.userCount || '0';
                    document.getElementById('petCount').textContent = data.petCount || '0';
                    document.getElementById('appointmentCount').textContent = data.appointmentCount || '0';
                    document.getElementById('recordCount').textContent = data.recordCount || '0';
                })
                .catch(error => {
                    console.error('加载统计数据失败:', error);
                    // 显示默认值
                    document.getElementById('userCount').textContent = '-';
                    document.getElementById('petCount').textContent = '-';
                    document.getElementById('appointmentCount').textContent = '-';
                    document.getElementById('recordCount').textContent = '-';
                });
        }

        function loadTodayStatistics() {
            fetch('${pageContext.request.contextPath}/dashboard/today-stats')
                .then(response => response.json())
                .then(data => {
                    if (data.error) {
                        console.error('获取今日统计数据失败:', data.error);
                        return;
                    }

                    document.getElementById('todayUsers').textContent = data.todayUsers || '0';
                    document.getElementById('todayPets').textContent = data.todayPets || '0';
                    document.getElementById('todayAppointments').textContent = data.todayAppointments || '0';
                    document.getElementById('pendingAppointments').textContent = data.pendingAppointments || '0';
                })
                .catch(error => {
                    console.error('加载今日统计数据失败:', error);
                    // 显示默认值
                    document.getElementById('todayUsers').textContent = '-';
                    document.getElementById('todayPets').textContent = '-';
                    document.getElementById('todayAppointments').textContent = '-';
                    document.getElementById('pendingAppointments').textContent = '-';
                });
        }
    </script>
</body>
</html>
